package com.unlcn.ils.kas.service.util;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;

public abstract class ExcelCommon {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelCommon.class);

    /**
     * 获取obj所有属性名称
     * @param obj
     */
    public static List<String> getAttributeName(Object obj) {
        Field[] fields = obj.getClass().getDeclaredFields();

        if (fields==null || fields.length==0)
            return null;

        List<String> list = new ArrayList<>();
        for (Field field : fields) {
            list.add(field.getName());
        }

        return list;
    }

    /**
     * 根据属性名获取属性值
     * @param obj
     */
    public static Object getFieldValueByFieldName(Object obj, String name) {

        try {
            Field field = obj.getClass().getDeclaredField(name);

            field.setAccessible(true);

            if (field.get(obj) == null)
                return "";

            Object val = field.get(obj);

            if (val==null || val.equals(""))
                return "";

            if (field.getType().toString().contains("java.util.Date")) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                val = simpleDateFormat.format(val);
            }

            return  val;
        } catch (Exception e) {
            LOGGER.error("ExcelCommon.getFieldValueByFieldName ERROR Message : {} ", e);
            return "";
        }
    }

    /**
     * 获取map所有key值
     * @param map
     * @return
     */
    public static List<String> getMapKey(Map map) {

        Set set = map.keySet();

        List<String> list = new ArrayList<>();
        for(Iterator iter = set.iterator(); iter.hasNext();){
            String key = (String)iter.next();
            list.add(key);
        }

        return list;
    }

    /**
     * 获取map所有val值
     * @param map
     * @return
     */
    public static List<String> getMapVal(Map map) {

        Set set = map.keySet();

        List<String> list = new ArrayList<>();
        for(Iterator iter = set.iterator(); iter.hasNext();){
            String key = (String)iter.next();
            String value = (String)map.get(key);
            list.add(value);
        }

        return list;
    }

    /**
     * 设置excel样式
     * @param workbook
     * @return
     */
    public static HSSFCellStyle createCellStyle(HSSFWorkbook workbook){

        HSSFCellStyle style = workbook.createCellStyle();

        style.setBorderTop(HSSFBorderFormatting.BORDER_THIN);
        style.setBorderBottom(HSSFBorderFormatting.BORDER_THIN);
        style.setBorderLeft(HSSFBorderFormatting.BORDER_THIN);
        style.setBorderRight(HSSFBorderFormatting.BORDER_THIN);

        style.setTopBorderColor(HSSFColor.RED.index);
        style.setBottomBorderColor(HSSFColor.RED.index);
        style.setLeftBorderColor(HSSFColor.RED.index);
        style.setRightBorderColor(HSSFColor.RED.index);
        style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

        return style;
    }
}
